const utils = require("../../../utils/utils")
const app = getApp()
// pages/print/list/list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isOut: -1,
        chooseIndex: 0,
        fixShow: false, //添加的view
        qShow: false, //控制优惠券列表展示
        docFixShow: false, //设置文档view
        num: 1,
        picFixShow: false, //设置图片 相片的view
        currentObj: {
            currentColor: 1,
            currentNum: 1,
            currentFace: 1,
            currentPaper: 'A4',
            currentFull: 1,
            currentbegin: 1,
            currentend: 1
        },
        type: 1,
        allPrice: 0.00,
        refreshState: false,
        payTypeList: [],
        without: false,
        firstOpen: 0,
        qlist: [],
        chooseImageArray: [],
        pictureList: [],
        isSet: 0,
        tipsShow: false,
        tipsFix: false
    },
    closeFixTips() {
        this.setData({
            tipsFix: false
        })
    },
    hideq() {
        this.setData({
            qShow: false
        })
    },
    //使用优惠券
    sy(e) {
        var that = this;
        var sn = e.currentTarget.dataset.id;
        var conponName = e.currentTarget.dataset.conname;
        // wx.showLoading()
        that.selectComponent("#loading").showLoadingCus();
        that.setData({
            currentObj: {}
        })
        var index = that.data.chooseIndex;
        var oldData = that.data.list[index];
        var obj = {};
        obj.conponName = conponName;
        obj.taskNo = oldData.taskOrderNo;
        obj.serviceMainId = oldData.serviceMainId;
        obj.serviceSubtypeId = oldData.serviceSubtypeId;
        obj.name = oldData.fileName
        obj.fileUrl = oldData.fileUrl;
        obj.printUrl = oldData.printUrl;
        obj.currentNum = oldData.num;
        obj.pageNum = oldData.pdfPageCount;
        obj.pageTotal = oldData.pageTotal;
        obj.orientation = oldData.orientation;
        var str = oldData.pageRange.split('-')
        obj.currentbegin = str[0];
        obj.currentend = str[1];
        obj.currentFace = oldData.singleDouble;
        obj.currentMargins = oldData.margins;
        obj.currentColor = oldData.color;
        obj.currentPaper = oldData.paper;
        obj.taskNo = oldData.taskOrderNo;
        obj.tagFloor = oldData.tagFloor;
        obj.couponListId = sn;
        app.get_local('machineQrCode').then(res => {
            var array = [{
                taskOrderNo: obj.taskNo,
                machineQrCode: res,
                serviceMainId: obj.serviceMainId,
                serviceSubtypeId: obj.serviceSubtypeId,
                pageTotal: obj.pageTotal,
                orientation: obj.orientation,
                fileName: obj.name,
                fileUrl: obj.fileUrl,
                printUrl: obj.printUrl,
                num: obj.currentNum,
                pageRange: obj.currentbegin + '-' + obj.currentend,
                singleDouble: obj.currentFace,
                color: obj.currentColor,
                paper: obj.currentPaper,
                margins: obj.tagFloor == 'photo' || obj.tagFloor == 'reductionTypesetting' ? obj.currentMargins : 1,
                couponListId: obj.couponListId,
                tagFloor: obj.tagFloor,
                conponName: conponName
            }]
            utils.request('printOrderTask/createTasks', array, function (res) {
                if (res.data.code == 200) {
                    that.setData({
                        qShow: false
                    })
                    that.selectComponent("#loading").hideLoadingCus();
                    that.getList()
                    that.hideFix();
                } else {
                    that.selectComponent("#loading").hideLoadingCus();
                    utils.toast(res.data.msg, 4000)
                }
            })
        })
    },
    //获取当前可用的优惠券
    getQ(e) {
        var that = this;
        var taskNo = e.currentTarget.dataset.id;
        var index = e.currentTarget.dataset.index;
        var couponid = e.currentTarget.dataset.couponid;
        // wx.showLoading({
        //     mask: true
        // })
        that.selectComponent("#loading").showLoadingCus();
        utils.request('printOrderTask/taskCouponLists/' + taskNo, {}, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                for (var i = 0; i < res.data.data.length; i++) {
                    if (res.data.data[i].lockStatus == '1') {
                        if (couponid != res.data.data[i].id) {
                            res.data.data.splice(i, 1)
                        }
                    }
                }
                that.selectComponent("#loading").hideLoadingCus();
                that.setData({
                    qlist: res.data.data,
                    qShow: true,
                    chooseIndex: index
                })
            } else {
                that.selectComponent("#loading").hideLoadingCus();
                utils.toast(res.data.msg)
            }
        })
    },
    //打开图片
    showPicFix() {
        var obj = this.data.currentObj
        if (obj.code == 'idPhoto') {
            obj.currentPaper = 'A4'
        } else {
            obj.currentPaper = 'P6'
        }
        this.setData({
            picFixShow: true,
            currentObj: obj
        })
    },
    //关闭图片设置
    hidePicFix() {
        this.setData({
            picFixShow: false
        })
    },
    //修改文档设置前先设置基础参数
    setdocFixData(e) {
        this.setData({
            currentObj: {}
        })
        var index = e.currentTarget.dataset.index;
        var oldData = this.data.list[index];
        var serviceSubtypeId = e.currentTarget.dataset.id;
        var coupon = e.currentTarget.dataset.coupon;
        var serviceMainId = e.currentTarget.dataset.fid;
        var conponName = e.currentTarget.dataset.conname;
        var obj = {};
        obj.coupon = coupon;
        obj.conponName = conponName;
        obj.serviceMainId = serviceMainId;
        obj.serviceSubtypeId = serviceSubtypeId;
        obj.name = oldData.fileName
        obj.fileUrl = oldData.fileUrl;
        obj.printUrl = oldData.printUrl;
        obj.currentNum = oldData.num;
        obj.pageNum = oldData.pdfPageCount;
        obj.pageTotal = oldData.pageTotal;
        obj.orientation = oldData.orientation;
        var str = oldData.pageRange.split('-')
        obj.currentbegin = str[0];
        obj.currentend = str[1];
        obj.currentFace = oldData.singleDouble;
        obj.currentMargins = oldData.margins;
        obj.currentColor = oldData.color;
        obj.currentPaper = oldData.paper;
        obj.taskNo = oldData.taskOrderNo;
        obj.tagFloor = oldData.tagFloor
        this.setData({
            currentObj: obj,
            type: 1
        })
        this.getMachineConfig();
    },
    //刚进来时展示设置页面
    showSetting() {
        var that = this;
        that.setData({
            without: false
        })
        //获取打印机相关配置
        that.getMachineConfig();
    },
    //获取机器和项目配置
    getMachineConfig() {
        var that = this;
        if (utils.is_define(that.data.serviceItem)) {
            var itemPaper = '';
            var itemColor = '';
            var itemSingleDouble = '';
            var endPaper = '';
            var endColor = '';
            var endSingleDouble = '';
            for (var i = 0; i < that.data.serviceItem.length; i++) {
                if (that.data.currentObj.tagFloor == 'document' || that.data.currentObj.tagFloor == 'reductionTypesetting') {
                    if (that.data.serviceItem[i].id == that.data.currentObj.serviceSubtypeId) {
                        itemPaper = that.data.serviceItem[i].config.paper
                        itemColor = that.data.serviceItem[i].config.color
                        itemSingleDouble = that.data.serviceItem[i].config.singleDouble
                    }
                } else {
                    if (that.data.serviceItem[i].id == that.data.currentObj.serviceMainId) {
                        itemPaper = that.data.serviceItem[i].config.paper
                        itemColor = that.data.serviceItem[i].config.color
                        itemSingleDouble = that.data.serviceItem[i].config.singleDouble
                    }
                }
            }

            endPaper = utils.getIntersection(itemPaper, that.data.machineConfig.paper)
            endColor = utils.getIntersection(itemColor, that.data.machineConfig.color)
            if (that.data.machineConfig.singleDouble == 2 && itemSingleDouble == 2) {
                endSingleDouble = 2
            } else {
                endSingleDouble = 1
            }
            //配置
            var matchingConfigObj = {
                SingleDouble: Array,
                color: Array,
                paper: Array
            };
            var machinePaperArray = [];
            var machineColorArray = [];
            var machineSingleDoubleArray = [];
            var machinePaper = endPaper;
            for (var i = 0; i < machinePaper.length; i++) {
                var obj = {};
                obj.paper = machinePaper[i]
                obj.name = machinePaper[i] == 'P6' ? '6寸' : machinePaper[i]
                machinePaperArray.push(obj)
            }
            var machineColor = endColor;
            for (var i = 0; i < machineColor.length; i++) {
                var obj = {};
                obj.color = machineColor[i]
                obj.name = machineColor[i] == 1 ? '黑白' : '彩色'
                machineColorArray.push(obj)
            }
            if (endSingleDouble == 1) {
                machineSingleDoubleArray = [{
                    singleDouble: '1',
                    name: '单面'
                }]
            } else {
                machineSingleDoubleArray = [{
                    singleDouble: '1',
                    name: '单面'
                },
                {
                    singleDouble: '2',
                    name: '双面'
                }
                ]
            }
            var currentColor = 0;
            var currentPaper = 0;
            for (var i = 0; i < machinePaperArray.length; i++) {
                if (machinePaperArray[i].paper.indexOf(that.data.currentObj.currentPpper) != '-1') {
                    currentPaper++
                }
            }
            for (var i = 0; i < machineColorArray.length; i++) {
                if (machineColorArray[i].color.indexOf(that.data.currentObj.currentColor) != '-1') {
                    currentColor++
                }
            }
            matchingConfigObj.paper = machinePaperArray;
            matchingConfigObj.color = machineColorArray;
            matchingConfigObj.SingleDouble = machineSingleDoubleArray
            that.setData({
                'currentObj.margins': that.data.machineConfig.margins,
                'currentObj.currentPaper': currentPaper > 0 ? that.data.currentObj.currentPaper : matchingConfigObj.paper[0].paper, //纸张
                'currentObj.currentColor': currentColor > 0 ? that.data.currentObj.currentColor : matchingConfigObj.color[0].color, //颜色
                'currentObj.config': matchingConfigObj,
                setShow: true
            })
        } else {
            // wx.showLoading({
            //     mask: true
            // })
            that.selectComponent("#loading").showLoadingCus();
            utils.request('config/getMachineConfig/' + wx.getStorageSync('machineQrCode'), {}, function (res) {
                var itemPaper = '';
                var itemColor = '';
                var itemSingleDouble = '';
                var endPaper = '';
                var endColor = '';
                var endSingleDouble = '';
                for (var i = 0; i < res.data.data.serviceItem.length; i++) {
                    if (that.data.currentObj.tagFloor == 'document' || that.data.currentObj.tagFloor == 'reductionTypesetting') {
                        if (res.data.data.serviceItem[i].id == that.data.currentObj.serviceSubtypeId) {
                            itemPaper = res.data.data.serviceItem[i].config.paper //项目纸张
                            itemColor = res.data.data.serviceItem[i].config.color //项目颜色
                            itemSingleDouble = res.data.data.serviceItem[i].config.singleDouble //项目单双面
                        }
                    } else {
                        if (res.data.data.serviceItem[i].id == that.data.currentObj.serviceMainId) {
                            itemPaper = res.data.data.serviceItem[i].config.paper
                            itemColor = res.data.data.serviceItem[i].config.color
                            itemSingleDouble = res.data.data.serviceItem[i].config.singleDouble
                        }
                    }
                }
                endPaper = utils.getIntersection(itemPaper, res.data.data.machine.paper)
                endColor = utils.getIntersection(itemColor, res.data.data.machine.color)
                if (res.data.data.machine.singleDouble == 2 && itemSingleDouble == 2) {
                    endSingleDouble = 2
                } else {
                    endSingleDouble = 1
                }
                //配置
                var matchingConfigObj = {
                    SingleDouble: Array,
                    color: Array,
                    paper: Array
                };
                var machinePaperArray = [];
                var machineColorArray = [];
                var machineSingleDoubleArray = [];
                var machinePaper = endPaper
                for (var i = 0; i < machinePaper.length; i++) {
                    var obj = {};
                    obj.paper = machinePaper[i]
                    obj.name = machinePaper[i] == 'P6' ? '6寸' : machinePaper[i]
                    machinePaperArray.push(obj)
                }
                var machineColor = endColor
                for (var i = 0; i < machineColor.length; i++) {
                    var obj = {};
                    obj.color = machineColor[i]
                    obj.name = machineColor[i] == 1 ? '黑白' : '彩色'
                    machineColorArray.push(obj)
                }
                if (endSingleDouble == 1) {
                    machineSingleDoubleArray = [{
                        singleDouble: '1',
                        name: '单面'
                    }]
                } else {
                    machineSingleDoubleArray = [{
                        singleDouble: '1',
                        name: '单面'
                    },
                    {
                        singleDouble: '2',
                        name: '双面'
                    }
                    ]
                }
                matchingConfigObj.paper = machinePaperArray;
                matchingConfigObj.color = machineColorArray;
                matchingConfigObj.SingleDouble = machineSingleDoubleArray
                var currentColor = 0;
                var currentPaper = 0;
                for (var i = 0; i < machinePaperArray.length; i++) {
                    if (machinePaperArray[i].paper.indexOf(that.data.currentObj.currentPpper) != '-1') {
                        currentPaper++
                    }
                }
                for (var i = 0; i < machineColorArray.length; i++) {
                    if (machineColorArray[i].color.indexOf(that.data.currentObj.currentColor) != '-1') {
                        currentColor++
                    }
                }
                matchingConfigObj.paper = machinePaperArray;
                matchingConfigObj.color = machineColorArray;
                matchingConfigObj.SingleDouble = machineSingleDoubleArray
                that.selectComponent("#loading").hideLoadingCus();
                that.setData({
                    'currentObj.margins': res.data.data.machine.margins,
                    'currentObj.config': matchingConfigObj,
                    'currentObj.currentPaper': currentPaper > 0 ? that.data.currentObj.currentPaper : matchingConfigObj.paper[0].paper, //纸张
                    'currentObj.currentColor': currentColor > 0 ? that.data.currentObj.currentColor : matchingConfigObj.color[0].color, //颜色
                    serviceItem: res.data.data.serviceItem,
                    setShow: true,
                    machineConfig: res.data.data.machine
                })
            })
        }

    },
    //关闭设置
    hideFix() {
        this.setData({
            setShow: false
        })
    },
    //打开再次添加文件
    showAdd() {
        this.setData({
            fixShow: true
        })
    },
    //关闭再次添加文件
    hideAdd() {
        this.setData({
            fixShow: false
        })
    },
    //文档设置完成
    //type  1 修改  2 新添
    docConfirm() {
        if (this.data.type == 1) {
            this.updateItem();
        }
        if (this.data.type == 2) {
            this.addList();
        }
    },
    previewOnSet() {
        var that = this;
        var url = that.data.currentObj.printUrl || that.data.currentObj.fileUrl;
        var title = that.data.currentObj.name;
        console.log(that.data.currentObj)
        var strs = url.split('.')
        // wx.showLoading()
        that.selectComponent("#loading").showLoadingCus();
        if (strs[strs.length - 1] == 'jpg' || strs[strs.length - 1] == 'png') {
            that.selectComponent("#loading").hideLoadingCus();
            wx.previewImage({
                current: url, // 当前显示图片的http链接
                urls: [url] // 需要预览的图片http链接列表
            })

        } else {
            that.selectComponent("#loading").hideLoadingCus();
            wx.navigateTo({
                url: '/pages/my/pdfsee/pdfsee?url=' + url + '&name=' + title
            })
        }
    },
    //修改设置
    updateItem() {
        var that = this;
        that.selectComponent("#loading").showLoadingCus();
        app.get_local('machineQrCode').then(res => {
            var array = [{
                taskOrderNo: that.data.currentObj.taskNo,
                machineQrCode: res,
                serviceMainId: that.data.currentObj.serviceMainId,
                serviceSubtypeId: that.data.currentObj.serviceSubtypeId,
                fileName: that.data.currentObj.name,
                conponName: that.data.currentObj.conponName,
                fileUrl: that.data.currentObj.fileUrl,
                printUrl: that.data.currentObj.printUrl,
                pageTotal: that.data.currentObj.pageTotal,
                orientation: that.data.currentObj.orientation,
                num: that.data.currentObj.currentNum,
                pageRange: that.data.currentObj.currentbegin + '-' + that.data.currentObj.currentend,
                singleDouble: that.data.currentObj.currentFace,
                color: that.data.currentObj.currentColor,
                paper: that.data.currentObj.currentPaper,
                margins: that.data.currentObj.tagFloor == 'photo' || that.data.currentObj.tagFloor == 'reductionTypesetting' ? that.data.currentObj.currentMargins : 1,
                couponListId: that.data.currentObj.coupon,
                tagFloor: that.data.currentObj.tagFloor
            }]
            utils.request('printOrderTask/createTasks', array, function (res) {
                if (res.data.code == 200) {
                    that.selectComponent("#loading").hideLoadingCus();
                    that.getList()
                    that.hideFix();
                    if (utils.is_define(res.data.data.tips)) {
                        wx.showModal({
                            title: '提示',
                            content: res.data.data.tips,
                            showCancel: false
                        })
                    }

                } else {
                    that.selectComponent("#loading").hideLoadingCus();
                    if (res.data.code = '-201') {
                        wx.showModal({
                            title: '提示',
                            content: res.data.msg,
                            showCancel: false
                        })
                    } else {
                        utils.toast(res.data.msg, 4000)
                    }
                }
            })
        })
    },
    //图片相片设置完成
    picConfirm() {
        this.hidePicFix();
        if (this.data.type == 1) {
            this.updateItem();
        }
        if (this.data.type == 2) {
            this.addList();
        }
    },
    // 数量变化
    add() {
        var num = this.data.currentObj.currentNum;
        num++;
        var obj = this.data.currentObj;
        obj.currentNum = num;
        this.setData({
            currentObj: obj
        })
    },

    lessen() {
        var num = this.data.currentObj.currentNum;
        if (num == 1) {
            utils.toast('最小份数为1')
            return false;
        }
        num--;
        var obj = this.data.currentObj;
        obj.currentNum = num;
        this.setData({
            currentObj: obj
        })
    },
    //单双面
    changeFace(e) {
        var index = e.currentTarget.dataset.index;
        var obj = this.data.currentObj;
        obj.currentFace = index;
        if (obj.code == '28542179') {
            //当为双面时 切换为A4
            if (index == 2) {
                obj.currentPaper = 'A4'
            }
        }
        this.setData({
            currentObj: obj
        })
    },
    //色彩
    changeColor(e) {
        var index = e.currentTarget.dataset.index;
        var obj = this.data.currentObj;
        obj.currentColor = index;
        this.setData({
            currentObj: obj
        })
    },
    // 纸张
    changePaper(e) {
        var index = e.currentTarget.dataset.index;
        var obj = this.data.currentObj;
        obj.currentPaper = index;
        if (obj.code == '28542179') {
            //当为彩色时 只能6寸 单面
            if (index == '52991176') {
                obj.currentColor = 2
                obj.currentFace = 1
            }
        }
        this.setData({
            currentObj: obj
        })
    },
    //设置个空白方法，阻止冒泡
    touchAnother() {

    },
    //获取再次添加的项目
    getAddItemList() {
        var that = this;
        utils.request('serviceItem/serviceItemLists/6', {}, function (res) {
            that.setData({
                addItemList: res.data.data
            })
        })
    },
    //打印单个删除
    listDetele(e) {
        var that = this;
        var newList = that.data.list;
        var index = e.currentTarget.dataset.index;
        var taskNo = e.currentTarget.dataset.taskno;
        wx.showModal({
            title: '提示',
            content: '确定要删除此项吗',
            confirmText: '确定',
            success(res) {
                if (res.confirm) {
                    // wx.showLoading()
                    that.selectComponent("#loading").showLoadingCus();
                    utils.request('printOrderTask/delTasks', {
                        "machineQrCode": wx.getStorageSync('machineQrCode'),
                        "taskOrderNo": taskNo
                    }, function (res) {
                        that.selectComponent("#loading").hideLoadingCus();
                        if (res.data.code == 200) {
                            newList.splice(index, 1)
                            that.setData({
                                list: newList
                            })
                            that.countPrice()
                        } else {
                            utils.toast(res.data.msg)
                        }
                    })
                }
            }
        })
    },
    //加入打印列表
    addList() {
        var that = this;
        that.selectComponent("#loading").showLoadingCus();
        app.get_local('machineQrCode').then(res => {
            // wx.showLoading()
            var array = [{
                taskOrderNo: "",
                machineQrCode: res,
                serviceMainId: that.data.currentObj.serviceMainId,
                serviceSubtypeId: that.data.currentObj.serviceSubtypeId,
                fileName: that.data.currentObj.name,
                fileUrl: that.data.currentObj.htFileUrl,
                printUrl: that.data.currentObj.fileUrl,
                num: that.data.currentObj.currentNum,
                orientation: that.data.currentObj.orientation,
                pageTotal: that.data.currentObj.pageNum,
                pageRange: that.data.currentObj.currentbegin + '-' + that.data.currentObj.currentend,
                singleDouble: that.data.currentObj.currentFace,
                color: that.data.currentObj.currentColor,
                paper: that.data.currentObj.currentPaper,
                margins: that.data.currentObj.tagFloor == 'photo' || that.data.currentObj.tagFloor == 'reductionTypesetting' ? that.data.currentObj.margins : 1,
                couponListId: "",
                tagFloor: that.data.currentObj.tagFloor,
                clothes: that.data.clothesId ? true : false,
                certificatesPhoto: that.data.certificatesPhoto ? true : false

            }]
            utils.request('printOrderTask/createTasks', array, function (res) {
                if (res.data.code == 200) {
                    that.getList()
                    that.hideFix();
                    that.selectComponent("#loading").hideLoadingCus();
                    if (utils.is_define(res.data.data.tips)) {
                        wx.showModal({
                            title: '提示',
                            content: res.data.data.tips,
                            showCancel: false
                        })
                    }
                } else {
                    that.selectComponent("#loading").hideLoadingCus();
                    if (res.data.code = '-201') {
                        wx.showModal({
                            title: '提示',
                            content: res.data.msg,
                            showCancel: false
                        })
                    } else {
                        utils.toast(res.data.msg, 4000)
                    }
                }
            })
        })
    },

    // 获取打印列表
    getList() {
        var that = this;
        utils.request('printOrderTask/taskLists', {}, function (res) {
            that.setData({
                refreshState: false
            })
            if (res.data.code == 200) {
                if (res.data.data.length > 0) {
                    that.setData({
                        list: res.data.data
                        // isOut:res.data.data.isOut,
                    })
                    that.countPrice();
                } else {
                    that.setData({
                        list: [],
                        // payTypeList: res.data.data.payTypeList,
                        // isOut:res.data.data.isOut,
                        allPrice: '0.00',
                        discount: 0
                    })
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    keyboardheightchange(e) {
        this.setData({
            keyboardH: e.detail.height
        })
    },
    //计算价格
    countPrice() {
        var that = this;
        var list = that.data.list;
        var price = 0.00;
        var discount = 0.00;
        for (var i = 0; i < list.length; i++) {
            price = parseFloat((parseInt(price * 100) / 100).toFixed(2)) + parseFloat((parseInt(list[i].amount * 100) / 100).toFixed(2))
            if (utils.is_define(list[i].discountAmount)) {
                discount = parseFloat((parseInt(discount * 100) / 100).toFixed(2)) + parseFloat((parseInt(list[i].discountAmount * 100) / 100).toFixed(2))
            }
        }
        that.setData({
            allPrice: price.toFixed(2),
            refreshState: false,
            discount: discount
        })
        wx.stopPullDownRefresh()
        wx.hideNavigationBarLoading()
    },
    //提交订单到支付页
    confirmOrder() {
        var that = this;
        if (that.data.list.length <= 0) {
            utils.toast('请先添加打印任务')
            return false;
        }
        if (wx.getStorageSync('minPrice') > 0 && that.data.allPrice < wx.getStorageSync('minPrice')) {
            wx.showModal({
                title: '提示',
                content: '任务金额低于当前设备起打价格' + wx.getStorageSync('minPrice') + '元，溢出金额将转换为积分',
                confirmText: '确定',
                cancelText: '取消',
                complete: (res) => {
                    if (res.confirm) {
                        var arr = [];
                        for (var i = 0; i < that.data.list.length; i++) {
                            arr.push(that.data.list[i].taskOrderNo)
                        }
                        var taskNo = arr.join(',');
                        wx.navigateTo({
                            url: '/pages/print/pay/pay?taskNo=' + taskNo + '&price=' + wx.getStorageSync('minPrice') + '&orderType=1',
                        })
                    }
                }
            })
        } else {
            var arr = [];
            for (var i = 0; i < that.data.list.length; i++) {
                arr.push(that.data.list[i].taskOrderNo)
            }
            var taskNo = arr.join(',');
            wx.navigateTo({
                url: '/pages/print/pay/pay?taskNo=' + taskNo + '&price=' + that.data.allPrice + '&orderType=1',
            })
        }
    },
    closefGoPay() {
        var that = this;
        that.setData({
            tipsShow: false
        })
        var arr = [];
        for (var i = 0; i < that.data.list.length; i++) {
            arr.push(that.data.list[i].taskNo)
        }
        var taskNo = arr.join(',');
        wx.navigateTo({
            url: '/pages/print/pay/pay?taskNo=' + taskNo + '&price=' + that.data.allPrice + '&orderType=1',
        })
    },
    //预览
    previewFile(e) {
        var url = e.currentTarget.dataset.url;
        var office = e.currentTarget.dataset.office;
        var title = e.currentTarget.dataset.title;
        // wx.showLoading()
        this.selectComponent("#loading").showLoadingCus();
        if (office != 'jpg' && office != 'png') {
            this.selectComponent("#loading").hideLoadingCus();
            wx.navigateTo({
                url: '/pages/my/pdfsee/pdfsee?url=' + url + '&name=' + title
            })
        } else {
            this.selectComponent("#loading").hideLoadingCus();
            wx.previewImage({
                current: url, // 当前显示图片的http链接
                urls: [url] // 需要预览的图片http链接列表
            })

        }
    },
    //起始页码减
    lessenBegin(e) {
        var page = e.currentTarget.dataset.page;
        if (page == 1) {
            utils.toast('起始页必须大于0');
            return false;
        } else {
            page--
            this.setData({
                'currentObj.currentbegin': page
            })
        }
    },
    //起始页码加
    addBegin(e) {
        var page = e.currentTarget.dataset.page;
        var pages = e.currentTarget.dataset.pages;
        if (page == pages) {
            utils.toast('起始页不能大于结束页')
            return false;
        } else {
            page++
            this.setData({
                'currentObj.currentbegin': page
            })
        }
    },
    //结束页码减
    lessenEnd(e) {
        var page = e.currentTarget.dataset.page;
        var pages = e.currentTarget.dataset.pages;
        if (page == 1) {
            utils.toast('结束页必须大于0');
            return false;
        } else if (page == pages) {
            utils.toast('结束页不能小于起始页');
            return false;
        } else {
            page--
            this.setData({
                'currentObj.currentend': page
            })
        }
    },
    giveBegin(e) {
        var begin = e.currentTarget.dataset.pages;
        this.setData({
            'currentObj.currentbegin': begin
        })
    },
    pageIntBegin(e) {

        var that = this;
        var num = e.detail.value;
        var page = e.currentTarget.dataset.page;
        var begin = e.currentTarget.dataset.pages;
        if (!utils.is_define(num)) {
            return
        }
        if (parseInt(num) < 1) {
            utils.toast('起始页必须大于0');
            that.setData({
                'currentObj.currentbegin': begin
            })
            return;
        }
        if (parseInt(num) > parseInt(page)) {
            utils.toast('起始页不能大于结束页');
            that.setData({
                'currentObj.currentbegin': page
            })
            return;
        }
        that.setData({
            'currentObj.currentbegin': num
        })

    },
    giveEnd(e) {
        var page = e.currentTarget.dataset.page;
        this.setData({
            'currentObj.currentend': page
        })
    },
    pageIntEnd(e) {
        var that = this;
        var num = e.detail.value;
        var page = e.currentTarget.dataset.page;
        var begin = e.currentTarget.dataset.pages;
        if (!utils.is_define(num)) {
            return
        }
        if (parseInt(num) > parseInt(this.data.currentObj.pageNum)) {
            utils.toast('结束页不能超出总页数');
            that.setData({
                'currentObj.currentend': page
            })
            return;
        }
        if (utils.is_define(num) && (parseInt(num) < 1)) {
            utils.toast('结束页必须大于0');
            that.setData({
                'currentObj.currentend': begin
            })
            return;
        }
        if (utils.is_define(num) && (parseInt(num) < parseInt(begin))) {
            utils.toast('结束页不能小于起始页');
            that.setData({
                'currentObj.currentend': begin
            })
            return;
        }
        that.setData({
            'currentObj.currentend': num
        })
        // if (page == 1) {
        //     utils.toast('结束页必须大于0');
        //     return false;
        // } else if (page == pages) {
        //     utils.toast('结束页不能小于起始页');
        //     return false;
        // } else {
        //     page--
        //     this.setData({
        //         'currentObj.currentend': page
        //     })
        // }
    },
    //结束页码加
    addEnd(e) {
        var page = e.currentTarget.dataset.page;
        if (page == this.data.currentObj.pageNum) {
            utils.toast('结束页不能超出总页数');
            return false;
        } else {
            page++
            this.setData({
                'currentObj.currentend': page
            })
        }
    },
    changePageBegin(e) {
        var val = e.detail.value;
        if (!utils.is_define(val)) {
            this.setData({
                'currentObj.currentbegin': 1,
                keyboardH: 0
            })
            return false;
        }
        if (parseInt(val) < 1) {
            utils.toast('起始页必须大于0')
            this.setData({
                'currentObj.currentbegin': 1,
                keyboardH: 0
            })
            return false;
        }
        if (parseInt(val) > this.data.currentObj.currentend) {
            utils.toast('起始页不能大于结束页')
            this.setData({
                'currentObj.currentbegin': 1,
                keyboardH: 0
            })
            return false;
        }
        if (parseInt(val) > this.data.currentObj.pageNum) {
            utils.toast('起始页不能超出总页数')
            this.setData({
                'currentObj.currentbegin': 1,
                keyboardH: 0
            })
            return false;
        }
        this.setData({
            'currentObj.currentbegin': val,
            keyboardH: 0
        })
    },
    changePageEnd(e) {
        var val = e.detail.value;
        if (!utils.is_define(val)) {
            this.setData({
                'currentObj.currentend': 1,
                keyboardH: 0
            })
            return false;
        }
        if (parseInt(val) < 0) {
            utils.toast('结束页必须大于1')
            this.setData({
                'currentObj.currentend': this.data.currentObj.pageNum,
                keyboardH: 0
            })
            return false;
        }
        if (parseInt(val) < this.data.currentObj.currentbegin) {
            utils.toast('结束页不能小于起始页')
            this.setData({
                'currentObj.currentend': this.data.currentObj.pageNum,
                keyboardH: 0
            })
            return false;
        }
        if (parseInt(val) > this.data.currentObj.pageNum) {
            this.setData({
                'currentObj.currentend': this.data.currentObj.pageNum,
                keyboardH: 0
            })
            utils.toast('结束页不能超出总页数')
            return false;
        }
        this.setData({
            'currentObj.currentend': val,
            keyboardH: 0
        })
    },
    keyboardh(e) {
        this.setData({
            keyboardH: e.detail.height
        })
    },

    //添加打印
    addAgain(e) {
        var pid = e.currentTarget.dataset.pid;
        var id = e.currentTarget.dataset.id;
        var ptag = e.currentTarget.dataset.ptag;
        var tag = e.currentTarget.dataset.tag;
        var support = e.currentTarget.dataset.support;
        console.log(tag)
        if (support == false) {
            utils.toast("QQ暂不支持此方式")
            return false;
        }
        var that = this;
        that.hideAdd();
        if (ptag == 'photo') {
            wx.navigateTo({
                url: '/pages/print/photoList/photoList?code=' + ptag + '&serviceMainId=' + pid + '&serviceSubtypeId=' + id + '&tagFloor=' + ptag,
            })
        }
        if (ptag == 'image') {
            wx.navigateTo({
                url: '/pages/print/picture/picture?code=' + ptag + '&serviceMainId=' + pid + '&serviceSubtypeId=' + id + '&tagFloor=' + ptag,
            })
        }
        if (tag == 'document_weChatDocument') {
            wx.chooseMessageFile({
                count: 1,
                type: 'file',
                extension: ['doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'pdf'],
                success(res) {
                    var reg = /^.*\.(?:xls|xlsx)$/i;
                    if (reg.test(res.tempFiles[0].name)) {
                        //excel
                        that.excelTpdf(res.tempFiles[0].path, res.tempFiles[0].name, tag, id, pid, ptag)
                    } else {
                        //非excel
                        // wx.showLoading()
                        that.selectComponent("#loading").showLoadingCus();
                        that.settingDoc(res.tempFiles[0].path, res.tempFiles[0].name, tag, id, pid, ptag)
                    }

                }
            })
        }
        if (tag == 'document_localDocument') {
            wx.navigateTo({
                url: '../local/local?code=document_localDocument&serviceMainId=' + pid + '&serviceSubtypeId=' + id + '&tagFloor=' + ptag,
            })
        }
    },
    //文档处理
    settingDoc(path, name, code, id, pid, ptag) {
        var that = this;
        utils.uploadfile(path, name, '', '', '', function (res) {
            var data = JSON.parse(res.data).data
            that.selectComponent("#loading").hideLoadingCus();
            var obj = {};
            obj.pageNum = data.pageTotal
            obj.taskOrderNo = '',
                obj.code = code
            obj.machineQrCode = wx.getStorageSync('machineQrCode'),
                obj.serviceMainId = pid,
                obj.serviceSubtypeId = id,
                obj.name = data.localName,
                obj.htFileUrl = data.fileUrl,
                obj.fileUrl = data.printUrl,
                obj.orientation = data.orientation
            obj.currentNum = 1,
                obj.currentbegin = 1,
                obj.currentend = data.pageTotal,
                obj.pageRange = '1-' + data.pageTotal,
                obj.currentFace = 1,
                obj.currentColor = 1,
                obj.currentPaper = 'A4',
                obj.margins = 1,
                obj.couponListId = "",
                obj.tagFloor = ptag
            that.selectComponent("#loading").hideLoadingCus();
            that.setData({
                currentObj: obj,
                type: 2
            })
            that.showSetting();
        })
    },
    excelTpdf(path, name, code, id, pid, ptag) {
        var that = this;
        // wx.showLoading()
        that.selectComponent("#loading").showLoadingCus();
        utils.uploadfileExcel(path, name, function (res) {
            var data = JSON.parse(res.data).data
            that.selectComponent("#loading").hideLoadingCus();
            if (data.length == 1) {
                wx.showActionSheet({
                    alertText: '请选择打印横竖版',
                    itemList: ['竖版', '横版'],
                    success(res) {
                        if (res.tapIndex == 0) {
                            // wx.showLoading()
                            that.selectComponent("#loading").showLoadingCus();
                            utils.uploadfile('', data[0].sheetName, data[0].downUrl, '', '1', function (res) {
                                var data = res.data.data
                                that.selectComponent("#loading").hideLoadingCus();
                                var obj = {};
                                obj.pageNum = data.pageTotal
                                obj.taskOrderNo = '',
                                    obj.code = code
                                obj.machineQrCode = wx.getStorageSync('machineQrCode'),
                                    obj.serviceMainId = pid,
                                    obj.serviceSubtypeId = id,
                                    obj.name = data.localName,
                                    obj.htFileUrl = data.fileUrl,
                                    obj.fileUrl = data.printUrl,
                                    obj.currentNum = 1,
                                    obj.currentbegin = 1,
                                    obj.currentend = data.pageTotal,
                                    obj.pageRange = '1-' + data.pageTotal,
                                    obj.currentFace = 1,
                                    obj.currentColor = 1,
                                    obj.currentPaper = 'A4',
                                    obj.margins = 1,
                                    obj.couponListId = "",
                                    obj.tagFloor = ptag
                                that.selectComponent("#loading").hideLoadingCus();
                                that.setData({
                                    currentObj: obj,
                                    type: 2
                                })
                                that.showSetting();
                            })
                        }
                        if (res.tapIndex == 1) {
                            // wx.showLoading()
                            that.selectComponent("#loading").showLoadingCus();
                            utils.uploadfile('', data[0].sheetName, data[0].downUrl, '', '2', function (res) {
                                var data = res.data.data
                                that.selectComponent("#loading").hideLoadingCus();
                                var obj = {};
                                obj.pageNum = data.pageTotal
                                obj.taskOrderNo = '',
                                    obj.code = code
                                obj.machineQrCode = wx.getStorageSync('machineQrCode'),
                                    obj.serviceMainId = pid,
                                    obj.serviceSubtypeId = id,
                                    obj.name = data.localName,
                                    obj.htFileUrl = data.fileUrl,
                                    obj.fileUrl = data.printUrl,
                                    obj.currentNum = 1,
                                    obj.currentbegin = 1,
                                    obj.currentend = data.pageTotal,
                                    obj.pageRange = '1-' + data.pageTotal,
                                    obj.currentFace = 1,
                                    obj.currentColor = 1,
                                    obj.currentPaper = 'A4',
                                    obj.margins = 1,
                                    obj.couponListId = "",
                                    obj.tagFloor = ptag
                                that.selectComponent("#loading").hideLoadingCus();
                                that.setData({
                                    currentObj: obj,
                                    type: 2
                                })
                                that.showSetting();
                            })
                        }
                    },
                })
            } else {
                wx.navigateTo({
                    url: '/pages/print/chooseSheet/chooseSheet?list=' + JSON.stringify(data) + '&serviceMainId=' + pid + '&serviceSubtypeId=' + id + '&tagFloor=' + ptag + '&code=' + code + '&isList=1',
                })
            }
        })
    },
    goBackZip() {
        wx.navigateBack()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.getList();
        that.getAddItemList();
        var zipTag = wx.getStorageSync('isZip');
        var sendTime = wx.getStorageSync('sendTime');
        var virtualMachine = wx.getStorageSync('virtualMachine')
        that.setData({
            minPrice: wx.getStorageSync('minPrice')
        })
        if (utils.is_define(zipTag)) {
            wx.removeStorageSync('isZip')
        } else {
            zipTag = ''
        }
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68',
                        lineH: 128,
                        showZipTips: zipTag,
                        sendTime:sendTime,
                        virtualMachine:virtualMachine
                    })
                } else {
                    that.setData({
                        pdbottom: '40',
                        lineH: 100,
                        showZipTips: zipTag,
                        sendTime:sendTime,
                        virtualMachine:virtualMachine
                    })
                }
            },
        })
        if (!utils.is_define(options.pageNum)) {
            return false;
        }
        var obj = {};
        obj.currentNum = 1;
        obj.tagFloor = options.tagFloor
        obj.pageNum = options.pageNum;
        obj.currentend = options.pageNum;
        obj.currentbegin = 1;
        if (options.old == 1) {
            obj.fileUrl = unescape(options.path)
        } else {
            obj.fileUrl = options.path;
        }
        obj.htFileUrl = options.fileUrl;
        obj.serviceMainId = options.serviceMainId;
        obj.serviceSubtypeId = options.serviceSubtypeId;
        obj.orientation = options.orientation || '1';
        obj.name = options.name;
        obj.code = options.code;
        obj.currentPaper = 'A4';
        if (obj.tagFloor == 'photo' || obj.tagFloor == 'idPhoto' || obj.code == 'reductionTypesetting_A4' || obj.code == 'reductionTypesetting_P6') {
            obj.currentColor = 2;
            obj.currentPaper = 'P6';
        } else {
            obj.currentColor = 1;
        }
        if (utils.is_define(options.color)) {
            obj.currentColor = options.color;
        }
        if (utils.is_define(options.clothesId)) {
            that.setData({
                clothesId: options.clothesId > 0 ? options.clothesId : ''
            })
        }
        if (utils.is_define(options.certificatesPhoto)) {
            that.setData({
                certificatesPhoto: options.certificatesPhoto
            })
        }
        obj.currentFull = -1;
        obj.currentFace = 1;
        this.setData({
            currentObj: obj,
            type: 2
        })
        this.showSetting();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (e) {
        var that = this;
        if (that.data.without) {
            that.showSetting();
        }
        if (that.data.firstOpen == 1) {
            that.getList();
        } else {
            that.setData({
                firstOpen: 1
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
    scrollTop() {

        this.setData({
            refreshState: true
        })
        this.getList();
    },
    onPulling(e) {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})